<?php
/**
 * Created by PhpStorm.
 * User: zty
 * Date: 2018/10/23
 * Time: 21:11
 */

namespace app\admin\controller;
use think\Controller;
use think\Request;

class Equipment extends Controller
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page =1 ,$pageSize = 10, $search =[])
    {
        $data = model('Common/Equipment')->getList($page,$pageSize,$search);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$data];
    }

    /**
     * 添加机构
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save($info=[])
    {
        $result = $this->validate(
            $info,
            'app\common\validate\Equipment'
        );

        if (true !== $result) {
            return ['code'=>1,'msg'=>$result];
        }

        $r = model('common/Equipment')->add_one($info);
        if($r){
            return ['code'=>0,'msg'=>'添加成功','id'=>$r];
        }else{
            return ['code'=>2,'msg'=>'添加失败'];
        }





    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $info = model('Common/Equipment')->get_one($id,['id','ins_id','name','code','address','lat','lng','type']);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$info];
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id,$info=[])
    {

        if(!$id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $result = $this->validate(
            $info,
            'app\common\validate\Equipment'
        );
        if (true !== $result) {
            return ['code'=>2,'msg'=>$result];
        }
        $r = model('Common/Equipment')->edit($id,$info);
        if($r){
            return ['code'=>0,'msg'=>'修改成功'];
        }else{
            return ['code'=>3,'msg'=>'修改失败'];
        }






    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!$id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $r = model('Common/Equipment')->del($id);
        if($r){
            return ['code'=>0,'msg'=>'删除成功'];
        }else{
            return ['code'=>2,'msg'=>'删除失败'];
        }
    }

    //关联视频设备
    public function relation(){
        $info = input('post.');
        $alarm = $info['alarm'];
        $data = model('Common/Equipment')->get_one($alarm);
        if(!$data) return ['code'=>2,'msg'=>'无报警设备'];
        if($data['type'] == 1) return ['code'=>2,'msg'=>'此设备不是报警设备'];
        $one = model('Common/EqRelation')->get_ong($alarm);
        if($one) return ['code'=>2,'msg'=>'该报警设备已经关联视频设备'];
        $r = model('Common/EqRelation')->add_one($info);
        if($r){
            return ['code'=>0,'msg'=>'绑定成功','data'=>$r];
        }else{
            return ['code'=>2,'msg'=>'绑定失败'];
        }
    }
    //通过报警设备id查询视频设备
    public function getVideo(){
        $info = input('post.');
        $alarm = $info['alarm'];
        $one = model('Common/EqRelation')->get_ong($alarm);
        if(!$one)return ['code'=>1,'msg'=>'该报警设备无视频设备'];
        return ['code'=>0,'msg'=>'查询成功','data'=>$one];
    }
    //通过视频设备id查询视频设备
    public function getAlarm(){
        $info = input('post.');
        $video = $info['video'];
        $one = model('Common/EqRelation')->get_video($video);
        if(!$one)return ['code'=>1,'msg'=>'该视频设备无报警设备'];
        return ['code'=>0,'msg'=>'查询成功','data'=>$one];
    }
    //解绑
    public function untying(){
        $info = input('post.');
        $alarm = $info['alarm'];
        model('Common/EqRelation')->del($alarm);
        return ['code'=>0,'msg'=>'解绑成功'];
    }

}
